package com.tomato.displaySample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class DIO_display extends Activity {

	Button dio_nextbutton,
			dio_prevbutton,
			dio_controlSetButton,
			dio_quitButton,
			dio_sequanceButton;
	
	Spinner dio_numberOfControllerSpiner;
	int currentPositionOfController;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.dioview);
	    // TODO Auto-generated method stub
	    Intent intent = getIntent();
	    int position = intent.getIntExtra("controllerNum", 0);
		ArrayAdapter<String> controller = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		controller.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		controller.add("選択してください。");
		controller.add("1	コントローラA");
		controller.add("3	コントローラB");
		controller.add("10	コントローラC");
		controller.add("30	テスト");
		controller.add("40	デモ用");
		controller.add("100	A");
		controller.add("128	B");
		
	    dio_prevbutton = (Button)findViewById(R.id.dio_prevbutton);
	    dio_nextbutton = (Button)findViewById(R.id.dio_nextbutton);
	    dio_sequanceButton = (Button)findViewById(R.id.dio_sequanceButton);
	    dio_controlSetButton = (Button)findViewById(R.id.dio_controlSetButton);
	    dio_quitButton = (Button)findViewById(R.id.dio_quitButton);
		   
	    dio_numberOfControllerSpiner = (Spinner)findViewById(R.id.dio_numberOfControllerSpiner);
	    dio_numberOfControllerSpiner.setAdapter(controller);
	    dio_numberOfControllerSpiner.setSelection(position, false);
	    
	    dio_prevbutton.setOnClickListener(button_Listener);
	    dio_nextbutton.setOnClickListener(button_Listener);
		dio_sequanceButton.setOnClickListener(button_Listener);
		dio_controlSetButton.setOnClickListener(button_Listener);
		dio_quitButton.setOnClickListener(button_Listener);

	    dio_numberOfControllerSpiner.setOnItemSelectedListener(spiner_Listener);
	    
	}
	
	private OnClickListener button_Listener=new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.dio_prevbutton:
				{
					finish();
					break;
				}
			case R.id.dio_nextbutton:
			{
				Intent intent=new Intent(DIO_display.this, AD_display.class);
				intent.putExtra("controllerNum2", currentPositionOfController);
				startActivity(intent);
				break;
			}
			case R.id.dio_sequanceButton:
			{	
				Intent intent=new Intent(DIO_display.this, sequance_display.class);
				startActivity(intent);
				break;
			}
			case R.id.dio_controlSetButton:		{
				Intent intent=new Intent(DIO_display.this, Control_display.class);
				startActivity(intent);
				break;
			}
			case R.id.dio_quitButton:		{
				break;
			}
			}
		}
	};
	
	private OnItemSelectedListener spiner_Listener = new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent,View view, int position, long id) {
			// TODO Auto-generated method stub
			currentPositionOfController = position;
			PopupMenu popupMenu = new PopupMenu(DIO_display.this, view);
			 popupMenu.getMenuInflater().inflate(R.menu.motor_popup,popupMenu.getMenu());
			 popupMenu.show();
		    // ポップアップメニューのメニュー項目のクリック処理
			 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
				 @Override
				 public boolean onMenuItemClick(MenuItem item) {
		            // 押されたメニュー項目名をToastで表示
		            Toast.makeText(DIO_display.this, item.getTitle(),  Toast.LENGTH_SHORT).show();
		            return true;
		        }
			 });
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
		
		
	};
}
